<template>
	<view class="content">
		<view class="header">
			<view class="face">
				<image :src="userInfo.avatar"></image>
			</view>
			<view class="info" v-if="hasLogin">
				<view class="name">
					<text class="username">{{ userInfo.realname}}</text>
					<!-- <view>
						<text class="yzb sex" :class="userInfo.sex==1?'yzb-nan':'yzb-nv'"></text>
					</view> -->
				</view>
				<view class="time" v-if="userInfo.vipLevel==0">未开通会员</view>
				<view class="time" v-else>{{formatCreateTime(userInfo.vipEndTime)}}到期，续费有效期顺延</view>
			</view>
		</view>
		<scroll-view class="tj-sction" scroll-x="true" :show-scrollbar="false">
			<view class="tj-item " :class="item.selected?'item-active':'item-normal'" v-for="(item,index) in items"
				:key="index" @click="selectGoods(index)">
				<text class="num">{{item.name}}</text>
				<view class="bottom">
					<text class="unit">¥</text>
					<text class="money">{{item.price}}</text>
				</view>
				<text class="days">{{item.remark}}</text>
			</view>
		</scroll-view>

		<view class="pay">
			<view class="pay-item" v-for="(item,index) in pays" :key="index" @click="selectPayType(index,item)">
				<view class="left">
					<image :src="item.icon"></image>
					<text>{{item.title}}</text>
				</view>
				<text class="right yzb"
					:class="item.selected?'yzb-yuanxingxuanzhongfill':'yzb-yuanxingweixuanzhong'"></text>
			</view>
		</view>
		
		<view class="equity" v-if="equityUrl">
			<text class="title">会员权益</text>
			<image :src="equityUrl" mode="widthFix" :style="{height:imgHeight+'px'}" @load="onImgLoad"></image>
		</view>

		<view class="btn">
			<button @click="pay">确认支付</button>
			<view class="protocol">
				<text>购买即视为同意</text>
				<text class="member-protocol" @click="toNav('/pages/my/vip/memberProtocol')">《会员服务协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex';
	import {
		formatDate
	} from '@/common/date';
	export default {
		computed: {
			...mapState(['userInfo']),
			...mapGetters(['hasLogin'])
		},
		data() {
			return {
				items: [],
				rightList: [
					 "权益类型",
					 "普通",
					 "黄金",
					 "铂金",
					 "钻石",
					 "钛金"
				],
				pays: [{
						id: "1",
						payType: "wxPay",
						title: '微信支付',
						icon: '../../../static/kp/weix.png',
						selected: false,
						active: true,
					},
					// {
					// 	id: "2",
					// 	payType:"aliPay",
					// 	title: '支付宝支付',
					// 	icon: '../../../static/kp/zfb.png',
					// 	selected: false,
					// 	active: false,
					// },
					{
						id: "3",
						payType: "jfPay",
						title: '积分支付',
						icon: '../../../static/kp/jf.png',
						selected: true,
						active: true,
					}
				],

				selectedItem: null,
				selectedPay: null,

				show: false,
				title: '温馨提示',
				content: '购买成功无法取消，确定支付？',
				
				equityUrl:"",
				imgHeight:0,
			}
		},

		onLoad() {
			this.selectedPay = this.pays[1];
			this.getVipList();
			// this.getPayTypeList();
		},

		methods: {

			getVipList() {
				this.$apis.getVipList({
					roleCode: this.userInfo.memberRole
				}).then(res => {
					console.log('getVipList', res);
					if (res) {
						res.forEach((item, index) => {
							if (index == 0) {
								item.selected = true
								this.selectedItem = item;
								this.equityUrl=item.equity;
							} else {
								item.selected = false;
							}
						})
						this.items = res;
					}
				});
			},

			getPayTypeList() {
				this.$apis.getPayTypeList().then(res => {
					console.log('getPayTypeList', res);
					if (res) {
						res.forEach((item, index) => {
							if (index == 0) {
								item.selected = true
							} else {
								item.selected = false;
							}
						})
						this.pays = res;
					}
				});
			},

			selectGoods(index) {
				for (var i = 0; i < this.items.length; i++) {
					this.items[i].selected = false;
				}
				this.items[index].selected = true;
				this.selectedItem = this.items[index];
			},

			selectPayType(index, item) {
				if (item.active == false) {
					uni.showToast({
						icon: 'none',
						title: "暂不支持"
					})
					return;
				}
				for (var i = 0; i < this.pays.length; i++) {
					this.pays[i].selected = false;
				}
				this.pays[index].selected = true;
				this.selectedPay = this.pays[index];
			},

			toNav(url) {
				uni.navigateTo({
					url: url
				});
			},

			confirm() {
				this.show = false;
				this.submit();
			},

			pay() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定支付？',
					success: res => {
						if (res.confirm) {
							that.submit();
						}
					}
				});
			},

			async submit() {
				let params = {
					payType: this.selectedPay.payType,
					vipId: this.selectedItem.id,
				}
				let res = await this.$apis.createVipOrder(params);
				if (res) {
					uni.showToast({
						icon: 'none',
						title: "购买成功"
					})
					//更新用户信息
					this.getUserInfo();
					setTimeout(() => {
						uni.navigateBack(-1);
					}, 1000);
				}
			},

			// 获取用户信息
			async getUserInfo() {
				let userInfo = await this.$apis.getUserInfo();
				this.$store.commit('SET_USERINFO', userInfo);
			},

			formatCreateTime(time) {
				if (time == null || time === '') {
					return null;
				}
				let str = time.replace(/-/g, '/');
				let date = new Date(str);
				return formatDate(date, 'yyyy-MM-dd');
			},
			
			
			onImgLoad(e){
				//图片加载完成以后，获取图片的原始宽度和高度，并根据宽度计算出高度
				let {width,height} = e.mp.detail;
				this.imgHeight=(height/width)*100;//高度=原始高度/原始宽度 *100
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.header {
		display: flex;
		flex-direction: row;
		align-items: center;
		width: 100%;
		padding: 30upx 20upx 80upx 20upx;
		box-sizing: border-box;
		background-color: $main-color;

		.face {
			image {
				width: 150upx;
				height: 150upx;
				border-radius: 75upx;
			}
		}

		.info {
			display: flex;
			flex-direction: column;
			padding-left: 30upx;
			box-sizing: border-box;
			align-items: flex-start;

			.name {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 10upx;
			}

			.username {
				width: 100%;
				color: #fff;
				font-weight: bold;
				font-size: 38upx;
				margin-right: 10rpx;
			}

			.yzb-nan {
				color: #f56c6c;
				font-weight: bold;
			}

			.yzb-nv {
				font-weight: bold;
				color: #f56c6c;
			}

			.time {
				// margin-top: 15upx;
				display: flex;
				height: 40upx;
				color: #ccf783;
				font-size: 26upx;
			}
		}
	}

	.rights {
		@extend %section;
		border-radius: 20upx;
		width: 96%;
		padding: 2% 2%;
		box-sizing: border-box;
		margin: 0 auto;
		margin-top: -50upx;
		white-space: nowrap;
		width: 100%;
		
		.grid-item-box {
			flex: 1;
			// position: relative;
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 15px 0;
		}
	}

	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	.tj-sction {
		margin-top: 30upx;
		@extend %section;
		border-radius: 20upx;
		width: 96%;
		padding: 5% 2%;
		box-sizing: border-box;
		margin: 0 auto;
		margin-top: -50upx;
		white-space: nowrap;
		width: 100%;

		.tj-item {
			display: inline-block;
			text-align: center;
			width: 200upx;
			font-size: 26upx;
			color: #75787d;
			border-radius: 10upx;
			margin-left: 20upx;
			padding: 30upx 0;
		}

		.item-normal {
			background-color: #f8f8f8;
		}

		.item-active {
			background-color: #F4CE98;
		}

		.num {
			font-size: 32upx;
			color: #666;
			margin-bottom: 10upx;
		}

		.days {
			font-size: 26upx;
			color: #999;
			margin-bottom: 25upx;
		}

		.unit {
			font-size: 26upx;
		}

		.money {
			margin-top: 30upx;
			font-size: 42upx;
			font-weight: bold;
		}
	}

	.pay {
		background-color: #fff;
		margin: 30upx 20upx;
		padding: 20upx;
		border-radius: 10upx;

		.pay-item {
			padding: 10upx 20upx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;

			.left {
				display: flex;
				flex-direction: row;
				align-items: center;

				image {
					width: 45upx;
					height: 45upx;
				}

				text {
					margin-left: 15upx;
					color: #333;
				}
			}

			.right {
				font-size: 44upx;
				color: #666;
			}

			.yzb-yuanxingxuanzhongfill {
				color: #12ae85;
			}

			.yzb-yuanxingweixuanzhong {
				color: #999;
			}

		}
	}
	
	.equity{
		background-color: #fff;
		margin: 30upx 20upx;
		padding: 20upx;
		border-radius: 10upx;
		display:flex;
		flex-direction:column;
		
		.title{
			font-weight: bold;
			padding: 10upx 0 20upx 0;
		}
		image{
			width: 100%;
			border-radius:10upx;
		}
	}

	.btn {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 100upx;
		padding-bottom: 50upx;

		button {
			margin: 0 auto;
			width: 90%;
			height: 80upx;
			line-height: 80upx;
			border-radius: 40upx;
			background-color: $main-color;
			color: #ffffff;
		}

		.protocol {
			font-size: 26upx;
			margin-top: 10upx;
			color: #999;

			.member-protocol {
				color: #12ae85;
			}
		}

	}
</style>